package com.mall.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.db.domain.Member;
import com.mall.db.domain.SendMessage;
import com.mall.db.service.MemberService;
import com.mall.db.service.SendMessageService;
import com.mall.utils.EmailUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class SendEmailJob implements BaseJob{

    @Resource(name = "sendMessageService")
    private SendMessageService sms;
    @Resource(name = "memberService")
    private MemberService ms;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        QueryWrapper<SendMessage> qw=new QueryWrapper<>();
        qw.eq("type","email").eq("state","1");
        List<SendMessage>list=sms.list(qw);
        for (SendMessage sm:list){
            QueryWrapper<Member>qm=new QueryWrapper<>();
            qm.eq("login_name",sm.getAcceptUser());
            Member m=ms.getOne(qm);
            if(m!=null&& !StringUtils.isEmpty(m.getEmail())){
                String emails[]={m.getEmail()};
                boolean b=EmailUtil.sendEmail(emails,sm.getContent(),sm.getTitle());
                if(b){
                    sm.setSendTime(new Date());
                    sm.setState(0);
                    sms.updateById(sm);
                }

            }
        }
    }
}
